package com.example.mysqltry.service.impl;

import com.example.mysqltry.api.vo.NewBeeMallIndexCategoryVO;
import com.example.mysqltry.api.vo.SecondLevelCategoryVO;
import com.example.mysqltry.api.vo.ThirdLevelCategoryVO;
import com.example.mysqltry.commom.Constants;
import com.example.mysqltry.commom.NewBeeMallCategoryLevelEnum;
import com.example.mysqltry.dao.MallGoodsCategoryMapper;
import com.example.mysqltry.entity.MallGoodsCategory;
import com.example.mysqltry.service.NewBeeMallCategoryService;
import com.example.mysqltry.util.BeanUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

@Service
public class NewBeeMallCategoryServiceImpl implements NewBeeMallCategoryService {
    @Autowired
    private MallGoodsCategoryMapper mallGoodsCategoryMapper;
    public List<NewBeeMallIndexCategoryVO> getCategoriesForIndex()
    {
        List<NewBeeMallIndexCategoryVO> newBeeMallIndexCategoryVOList=new ArrayList<>();
        List<MallGoodsCategory> firstCategoryList=mallGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(0L), NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel(), Constants.INDEX_CATEGORY_NUMBER);
        if(!CollectionUtils.isEmpty(firstCategoryList))
        {
            List<Long> firstLevelCategoryIds = firstCategoryList.stream().map(MallGoodsCategory::getCategoryId).collect(Collectors.toList());
            List<MallGoodsCategory> secondCategoryList=mallGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(firstLevelCategoryIds, NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel(), 0);
            if(!CollectionUtils.isEmpty(secondCategoryList))
            {
                List<Long> secondLevelCategoryIds = secondCategoryList.stream().map(MallGoodsCategory::getCategoryId).collect(Collectors.toList());
                List<MallGoodsCategory> thirdCategoryList=mallGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(secondLevelCategoryIds, NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel(), 0);
                if(!CollectionUtils.isEmpty(thirdCategoryList)) {
                    List<SecondLevelCategoryVO> secondLevelCategoryVOS=new ArrayList<>();
                    Map<Long, List<MallGoodsCategory>> thirdLevelCategoryMap = thirdCategoryList.stream().collect(groupingBy(MallGoodsCategory::getParentId));
                    for(MallGoodsCategory secondCategory : secondCategoryList)
                    {
                        SecondLevelCategoryVO secondLevelCategoryVO=new SecondLevelCategoryVO();
                        BeanUtil.copyProperties(secondCategory, secondLevelCategoryVO);

                        if(thirdLevelCategoryMap.containsKey(secondCategory.getCategoryId()))
                        {


                            List<MallGoodsCategory> temp=thirdLevelCategoryMap.get(secondCategory.getCategoryId());
                            List<ThirdLevelCategoryVO> thirdLevelCategoryVOS= BeanUtil.copyList(temp,ThirdLevelCategoryVO.class);
                            secondLevelCategoryVO.setThirdLevelCategoryVOS(thirdLevelCategoryVOS);
                            secondLevelCategoryVOS.add(secondLevelCategoryVO);
                        }

                    }
                  //  System.out.println(secondLevelCategoryVOS+"secondLevelCategoryVOS");
                    if(!CollectionUtils.isEmpty(secondLevelCategoryVOS)) {

                        Map<Long, List<SecondLevelCategoryVO>> secondLevelCategoryMap = secondLevelCategoryVOS.stream().collect(groupingBy(SecondLevelCategoryVO::getParentId));

                        for(MallGoodsCategory firstCategory : firstCategoryList) {
                            NewBeeMallIndexCategoryVO temp = new NewBeeMallIndexCategoryVO();

                            BeanUtil.copyProperties(firstCategory, temp);

                            if (secondLevelCategoryMap.containsKey(firstCategory.getCategoryId()))
                            {
                               List<SecondLevelCategoryVO> secondLevelCategoryVOS1=secondLevelCategoryMap.get(firstCategory.getCategoryId());
                                temp.setSecondLevelCategoryVOS(secondLevelCategoryVOS1);
                                newBeeMallIndexCategoryVOList.add(temp);
                            }
                        }
                        }

                    }


                }

            }

        return newBeeMallIndexCategoryVOList;
    }
}
